﻿@using DormMS.Model.Enum

@{
    Layout = null;
}

<h3>抽奖记录</h3>

<div class="panel panel-default" ng-controller="DrawRecordCtrl" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
    <div class="panel-heading">
        <div class="form-inline ng-pristine ng-valid">
            <div class="form-group" style="width:100%;">
                <input style="width: 15%;" placeholder="关键字" class="form-control" type="text" ng-model="Keywords">

                <div class="input-group">
                    <input readonly="readonly" placeholder="抽奖时间(从)" style="z-index: 0;" type="text" class="form-control" datetime-picker="yyyy-MM-dd HH:mm" ng-model="AddTimeFirst"
                           is-open="picker1.open" datepicker-options="datepickerOptions" />
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-default" ng-click="openCalendar($event, 'picker1')"><i class="fa fa-calendar"></i></button>
                    </span>
                </div>
                ～
                <div class="input-group">
                    <input readonly="readonly" style="z-index: 0;" placeholder="抽奖时间(到)" type="text" class="form-control" datetime-picker="yyyy-MM-dd HH:mm" ng-model="AddTimeSec"
                           is-open="picker2.open" datepicker-options="datepickerOptions" />
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-default" ng-click="openCalendar($event, 'picker2')"><i class="fa fa-calendar"></i></button>
                    </span>
                </div>
                <select ng-model="IsWin" class="form-control">
                    <option value="0">中奖</option>
                    <option value="1">未中奖</option>
                    <option value="2">全部</option>
                </select>
                <button type="submit" class="btn btn-labeled btn-primary" ng-click="tableParams.$params.page = 1;tableParams.reload()">
                    <span class="btn-label">
                        <em class="fa fa-search"></em>
                    </span>搜索
                </button>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                <colgroup>
                    <col style="width: 15%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 20%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 10%" />
                    <col style="width: 15%" />
                </colgroup>
                <tbody>
                    <tr ng-repeat="item in $data">
                        <td data-title="'奖项名'" class="text-center">{{item.DrawRewardNameM}}</td>
                        <td data-title="'会员昵称'" class="text-center">{{item.MemberName}}</td>
                        <td data-title="'是否中奖'" sortable="'IsWinning'" class="text-center">{{item.IsWinningM}}</td>
                        <td data-title="'商品类别'" sortable="'RewardType'" class="text-center">{{item.RewardTypeM}}</td>
                        <td data-title="'地址'" class="text-center">{{item.ProvinceM+'  '+item.CityM+'  '+item.DistrictM+'  '+'详细地址：  '+item.AddressM+'  '+'电话： '+item.MobileM}}</td>
                        <td data-title="'处理步骤'" sortable="'StepType'" class="text-center">{{item.StepTypeM}}</td>
                        <td data-title="'抽奖时间'" sortable="'AddTime'" class="text-center">{{item.AddTime}}</td>
                        <td data-title="'操作'" nowrap="nowrap" class="text-center">
                            <button type="button" ng-click="setStep(item.Id,@((int)StepType.待发放))" class="btn btn-sm btn-success" ng-show="item.RewardType==@((int)RewardType.实体商品)&&item.StepType==@((int)StepType.已发放)" title="更改状态为待发放">
                                <em class="fa fa-edit"></em>
                            </button>
                            <button type="button" ng-click="setStep(item.Id,@((int)StepType.已发放))" class="btn btn-sm btn-success" ng-show="item.RewardType==@((int)RewardType.实体商品)&&item.StepType==@((int)StepType.待发放)" title="更改状态为已发放">
                                <em class="fa fa-edit"></em>
                            </button>
                            <button ng-click="deleteRecord(item.Id)" type="button" class="btn btn-sm btn-danger" title="删除">
                                <em class="fa fa-trash-o"></em>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="panel-footer text-right">
        <button type="button" class="btn btn-warning" ng-click="cancel()">返回列表</button>
    </div>
    <script type="text/ng-template" id="modalDialogId">
        <div class="ngdialog-message text-center">
            <h3>确定要删除吗?</h3>
            <p>本记录关系到用户端的展示，删除后不可恢复!</p>
            <br />
            <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
            <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
        </div>
    </script>
    <script type="text/ng-template" id="addressDialogId">
        <div class="ngdialog-message text-center">
            <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">返回</button>
        </div>
    </script>
</div>


<script>
    App.controller('DrawRecordCtrl', [
   '$scope', '$resource', '$state', '$route', 'ngDialog', '$http', 'ngTableParams', 'ngTableDataService', 'Notify', '$modal', "$filter", "$timeout", "$stateParams", "$location",
   function ($scope, $resource, $state, $route, ngDialog, $http, ngTableParams, ngTableDataService, Notify, $modal, $filter, $timeout, $stateParams, $location) {
       'use strict';
       $scope.IsLoading = true;
       $scope.IsWin = 0;
       $scope.tableParams = new ngTableParams({
           page: 1,
           count: 10,
           sorting: {
               //Id: 'desc'     // 初始化排序
           }
       }, {
           getData: function ($defer, params) {
               $scope.isLoading = true;
               var requestParams = {
                   Skip: (params.page() - 1) * params.count(),
                   Take: params.count(),
                   Keywords: $scope.Keywords,
                   IsWin: $scope.IsWin,
                   AddTimeFirst: $scope.AddTimeFirst,
                   AddTimeSec: $scope.AddTimeSec,
                   DrawId: $stateParams.Id
               };
               $http.post('/Draw/GetRecordList', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                   .success(function (data, status) {

                       params.total(data.Total);
                       $defer.resolve(data.Results);

                       //记录查询参数，后续返回使用
                       $scope.urlParams = {
                           page: params.page(),
                           Keywords: $scope.Keywords,
                           AddTimeFirst: $scope.AddTimeFirst,
                           AddTimeSec: $scope.AddTimeSec,
                           DrawId: $stateParams.Id,
                           //FirstCategoryId: $scope.FirstCategoryId,
                           //SecondCategoryId: $scope.SecondCategoryId,
                           IsWin: $scope.IsWin
                       }
                       $scope.isLoading = false;
                   });


           }
       });

       //设置奖品发放状态
       $scope.setStep = function (id, type) {
           $http.post("/Draw/ModifyRecordStep", { id: id, type: type }).then(function (returnData) {
               if (returnData.data.isSuccess) {
                   $scope.tableParams.reload();
                   Notify.alert(returnData.data.msg, { status: 'success', timeout: 1000 });
               } else {
                   Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
               }
           });
       }

       $scope.showAddress = function (id) {

       }

       //退回列表页
       $scope.cancel = function () {
           $state.go("app.draw");
       }
       
       //删除记录
       $scope.deleteRecord = function (id) {
           ngDialog.openConfirm({
               template: 'modalDialogId',
               className: 'ngdialog-theme-default'
           }).then(function (value) {
               $http.post("/Draw/DeleteRecord", { id: id }).then(function (returnData) {
                   if (returnData.data.isSuccess) {
                       $scope.tableParams.reload();
                       Notify.alert(returnData.data.msg, { status: 'success', timeout: 1000 });
                   } else {
                       Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
                   }
               });
           }, function (reason) {
           }
            );
       }
       

       // 日期插件
       $scope.picker1 = {
           date: new Date()
       };

       $scope.picker2 = {
           date: new Date()
       };

       $scope.datepickerOptions = {
           showWeeks: false,
           startingDay: 1,
           dateDisabled: function (data) {
               //return (data.mode === 'day' && (new Date().toDateString() == data.date.toDateString()));
           }
       }
       $scope.openCalendar = function (e, picker) {
           $scope[picker].open = true;
       };

   }]);
</script>
